/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;

import com.cea.papyrus.core.PapyrusPlugin;

// TODO: Auto-generated Javadoc
/**
 * this is the figure to represent a class.
 * 
 * @author Patrick Tessier
 * @since 13 september 2006
 */
public class ClassifierFigure extends NodeFigure {

	/** The content pane. */
	private Figure contentPane;
	/* to present the class as an active class*/
	/** The active. */
	private boolean active=false;

	/* this is the containe of properties*/
	/** The property content. */
	private ContainerFigure propertyContent;
	
	/* this is the container of operations*/
	/** The method content. */
	private ContainerFigure methodContent;
	
	/** The invisible container  is a figure that used to detect the class figure whith a layout*/
	protected Figure invisibleContainer;
	
	/**
	 * this the constructor of a class figure.
	 */
	public ClassifierFigure() {
		super();
		ToolbarLayout layout = new ToolbarLayout();

		this.setLayoutManager(layout);
		layout.setSpacing(2);

		this.setOpaque(true); // non-transparent figure
		//setBorder( new LineBorder(ColorConstants.black));
		this.setBorder( new LineBorder(this.borderColor));
	
		// The area accepting inner figures.
		this.createContentPane();
		
		// the invisble container is created and it transparent*/
		this.invisibleContainer=new Figure();
		this.invisibleContainer.setOpaque(false);
		this.invisibleContainer = new FreeformLayer();
		this.invisibleContainer.setLayoutManager(new FreeformLayout());
		this.add(this.invisibleContainer);
	}

	/**
	 * To display a class as active.
	 * 
	 * @param b true if the clas is active
	 */
	public void  setActive(boolean b){
		this.active=b;
	}
	
	/**
	 * add the property container in the class figure.
	 * 
	 * @param propertyContent the poperty container
	 */ 
	public void addpropertyContainer(ContainerFigure propertyContent){
		this.propertyContent=propertyContent;
	}

/**
 * add the method container in the class figure.
 * 
 * @param methodContent the method container
 */
	public void addmethodContainer(ContainerFigure methodContent){
		this.methodContent=methodContent;
	}

	/**
	 * to obtain the property container of the class figure.
	 * 
	 * @return property container of the class figure
	 */
	public ContainerFigure getPropertyContent(){
		return this.propertyContent;}

	/**
	 * to obtain the method container of the class figure.
	 * 
	 * @return method container of the class figure
	 */
	public ContainerFigure getMethodContent(){
		return this.methodContent;}

	/**
	 * Get the inner figure accepting childs figure.
	 * 
	 * @return the contentPane of the class
	 */
	public IFigure getContentPane()
	{
		return this.contentPane;	
	}
	
	/**
	 * used to create a container to add child figure in the class.
	 */

	protected void createContentPane()
	{
		this.contentPane = new Figure();
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		this.contentPane.setLayoutManager(layout);
		this.add(this.contentPane);

	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
	 */
	/**
	 * Sets the bounds.
	 * 
	 * @param rect the rect
	 */
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		int propertyHeight=0;
		
		

		// calculate property Content size
		if (this.getPropertyContent()!=null){

			for (int i=0; i<this.getPropertyContent().getChildren().size();i++){
				propertyHeight=propertyHeight+((IFigure)this.getPropertyContent().getChildren().get(i)).getPreferredSize().height;
			}
			propertyHeight=propertyHeight+2;
			if (propertyHeight<15){propertyHeight=15;}
			this.getPropertyContent().setPreferredSize(30, propertyHeight);
		}
		int methodHeight=rect.height;

		if (this.getStereotypesLabel() != null)
		{methodHeight=methodHeight-this.getStereotypesLabel().getPreferredSize().height;}
		if (this.getQualifiedNameLabel() != null){
			{methodHeight=methodHeight-this.getQualifiedNameLabel().getPreferredSize().height;}
		}
		//calculate method content size
		methodHeight=methodHeight-this.getNameLabel().getPreferredSize().height;
		methodHeight=methodHeight-propertyHeight;
		if(this.getMethodContent()!=null){
			this.getMethodContent().setPreferredSize(30, methodHeight);
		}

	}
	
	/**
	 * Gets the invible container.
	 * 
	 * @return the invible container
	 */
	public IFigure getInvibleContainer(){
		return invisibleContainer;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#paint(org.eclipse.draw2d.Graphics)
	 */
	/**
	 * Paint.
	 * 
	 * @param graphics the graphics
	 */
	@Override
	public void paint (Graphics graphics) {
		super.paint (graphics);
		Figure f;
		Rectangle rect;

		// the class is not active
		if (this.active==false){
			f = this.getPropertyContent();
			if (f!=null) {
				rect = f.getBounds();
				graphics.setForegroundColor(this.getBorderColor());
				graphics.setBackgroundColor(this.getBorderColor());
				// draw speration between name and properties
				graphics.drawLine(rect.x, rect.y, rect.x+rect.width, rect.y);
				
				//draw separation between properties and operations
				graphics.drawLine(rect.x, rect.y+rect.height, rect.x+rect.width, rect.y+rect.height);
			}
		}
		
		// the class is active
		else{
			f = this.getPropertyContent();
			if (f!=null) {
				rect = f.getBounds();
				graphics.setForegroundColor(this.getBorderColor());
				graphics.setBackgroundColor(this.getBorderColor());
//				 draw speration between name and properties
				graphics.drawLine(rect.x+4, rect.y, rect.x+rect.width-4, rect.y); 
//				draw separation between properties and operations
				graphics.drawLine(rect.x+4, rect.y+rect.height, rect.x+rect.width-4, rect.y+rect.height);
			}


			//vertical lines for represents active
			rect =this.getBounds().getCopy();
			graphics.setLineWidth(2);
			graphics.setForegroundColor(this.getBorderColor());
			graphics.setBackgroundColor(this.getBorderColor());
			graphics.drawLine(rect.x+1, rect.y, rect.x+1,  rect.y+rect.height);
			graphics.drawLine(rect.x+4, rect.y, rect.x+4,  rect.y+rect.height);

			graphics.drawLine(rect.x+rect.width-1, rect.y,rect.x+rect.width-1,  rect.y+rect.height);
			
			graphics.drawLine(rect.x+rect.width-5, rect.y,rect.x+rect.width-5,  rect.y+rect.height);
		}
	}
	
	/**
	 * to display the Class as abstract.
	 * 
	 * @param b yes for abstract no other
	 */
	public void setAbstract(boolean b){
		
		if (b) {
			FontData[] font={new FontData(getfigureFont(), getFontSize(), SWT.ITALIC|SWT.BOLD)};
			this.getNameLabel().setFont(PapyrusPlugin.fontManager.get(font));//italic
		} else {
			FontData[] font={new FontData(getfigureFont(), getFontSize(), SWT.BOLD)};
			this.getNameLabel().setFont( PapyrusPlugin.fontManager.get(font));
		}
	}
}
